
const UnReadMsgs = require('./UnReadMsgs')
const msgHelp=require('../msgs/msgHelp')
const GroupHelp=require('../groups/GroupHelp')



//写入消息
exports.saveUnReadMsg=async (obj) => {

    let time=new Date().getTime()
    let newObj=Object.assign(obj,{time:time})

    let msg=new UnReadMsgs(newObj)
    await msg.save()
    return ''
}



//获取未读消息
const getUnReadMsg=async (to_uid) => {

    let msg=await UnReadMsgs.findOne({to_uid:to_uid})
    return msg
}
exports.getUnReadMsg=getUnReadMsg



//获取未读消息的数量
exports.getUnReadMsgNum=async (to_uid,from_uid) => {

    let msg=await UnReadMsgs.findOne({to_uid:to_uid})

    from_uid=from_uid+""

    if(msg){
        if(msg.msg_ids[from_uid]){
            return msg.msg_ids[from_uid].length
        }
        else{
            return 0
        }
    }
    else{
        return 0
    }
}



//获取未读消息的总数
exports.getUnReadMsgNumTotal=async (to_uid) => {

    let msg=await UnReadMsgs.findOne({to_uid:to_uid})
    let num=0
    if(msg){
        num=msg.unRead
    }

    return num
}



//更新未读消息
const updateUnReadMsg=async (obj,to_uid) => {

    let time=new Date().getTime()
    let newObj=Object.assign(obj,{time:time,})

    await UnReadMsgs.update({to_uid:to_uid},newObj)

    return ''
}
exports.updateUnReadMsg=updateUnReadMsg



//获取未读的消息数组，通过mid查找
exports.getMsgsByMid=async (to_uid) => {

   let msgIdList=await getMids(to_uid)

   if(msgIdList.length>0){
       let msglist=[]
       for(let i=0; i<msgIdList.length; i++ ){
           let mid=msgIdList[i]
           let msg=await msgHelp.findMsg(mid)
           msglist.push(msg)
       }
       return msglist
   }
   else{
       return []
   }
}



//移除群未读消息
exports.removeMsgByGid=async (gid) => {
    let useridlist=await GroupHelp.getGroupUserIds(gid)
    if(useridlist.length>0) {
        for(let userid of useridlist){
          let res=await getUnReadMsg(userid)
            if(res){

                let msg_ids=res.msg_ids
                let msgs_ids_gid=msg_ids[gid+""]
                if(msgs_ids_gid&&msgs_ids_gid.length>0){
                    let msgs_ids_copy=JSON.parse(JSON.stringify(msg_ids))
                    await removeGidItem(msgs_ids_copy,gid,userid,res)
                }
            }
        }
    }
    return  ''
}



//移除未读消息中的与参数gid相关的未读数
const removeGidItem=async (msgs_ids,gid,userid,res) => {
    gid=gid+""
    let msgs_ids_copy=JSON.parse(JSON.stringify(msgs_ids))
    let len=msgs_ids_copy.length
    msgs_ids[gid]=[]
    let unRead=res.unRead-len
    let new_msg=Object.assign({},res,{msg_ids:msgs_ids,unRead})
    updateUnReadMsg(new_msg,userid)
}



const getMids=async (to_uid) => {

    let res=await getUnReadMsg(to_uid)

    if(res){
        let msgIdList=[]
        let msg_ids=res.msg_ids

        //二维数组
        let midArr_2d=Object.values(msg_ids)

        for(let midArr of midArr_2d){
            for(let item of midArr){
                msgIdList.push(item)
            }
        }

        return msgIdList

    }
    else{
        return []
    }
}
exports.getMids=getMids



//谁给我发了消息
exports.getUsers=async (to_uid) => {

    let res=await getUnReadMsg(to_uid)

    if(res){
        let msg_ids = res.msg_ids
        let users = []
        let keys = Object.keys(msg_ids)
        /** 过滤掉已读的用户 */
        for (let i = 0; i < keys.length; i ++) {
            let key = keys[i]
            if (msg_ids[key].length > 0) {
                users.push(key)
            }
        }
        return users

    }
    else {
        return []
    }
}


